﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;

using DomainModel.Abstract;
using DomainModel.Concrete;
using DomainModel.Entities;
using WebCore.ModelViews;


namespace WebController.Controllers
{
    public class TagController: _baseController
    {
        private readonly TagsRepository _tagRepository;
        private readonly IEventTagsRepository _eventTagsRepository;

        public TagController()
        {
            _tagRepository = new TagsRepository();
            _eventTagsRepository = new EventTagsRepository();
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult List(string TagName)
        {
            if (!string.IsNullOrEmpty(TagName))
            {
                //_tagRepository.
                var tagsList = _tagRepository.GetByName(TagName);

                var eventsList = new List<Event>();

                if (tagsList != null)
                {
                    var tags = tagsList.Where(x => x.EventsList.Count != 0);

                   
                    foreach (var t in tags)
                    {
                         var item = t.EventsList.OfType<Event>().FirstOrDefault();

                         if (item != null)
                         {
                             eventsList.Add(item);
                         }
                    }

                }

                // page info setup
                var resultsView = new ResultsView{
                     PageTitle = "Tag",
                      QueryString = TagName
                 };
                ViewData["ResultsView"] = resultsView;

                // return the related events data
                return View ("EventsList", eventsList);

            }
            else
            {
                RedirectToAction("PageNotFound", "Error");
            }

            return null;
        }
    }
}
